
/** 装饰器 */
export function decorator<P = any>(...fns: ((e: any) => any)[]) {
    return (puppet: React.ComponentType<P>) => compose.apply(null, [...fns])(puppet) as React.FC<P>;
}

/**
 * 组合函数（同步）
 * @param fns 
 */
const compose = <In, Out>(...fns: Fns) => (value?: In): Out => {
    let re: any = value;
    for (const i in fns) {
        if (fns[i]) {
            const newValue = fns[i](re);
            if (newValue) {
                re = newValue;
            } else {
                return re;
            }
        }
    }
    return re;
}

/** 函数数组 */
type Fns = Array<(value: any) => Promise<any> | any>;